<?php
namespace App\Http\Controllers\Game;

use App\Http\Controllers\Controller;
use App\Models\Game\BankRuptOption;
use App\Models\Game\LogonAward;
use Illuminate\Http\Request;

class ConfController extends Controller
{
    public function index(Request $request) {

        if ($request->ajax()) {

            $rows = $request->get("rows");

            $LogonAward = new LogonAward();

            $paginator = $LogonAward->orderBy($request->get("sidx"), $request->get("sord"))->paginate($rows);

//             $bankRuptOptions = BankRuptOption::all();

            $rows = [];
            foreach ($paginator as $page) {
//                 foreach ($bankRuptOptions as $bankRuptOption) {
//                     if ($page->KindID == $bankRuptOption->KindID) {
//                         $page->PCGiveGold = $bankRuptOption->PCGiveGold;
//                     }
//                 }
                $rows[] = $page;
            }

            $data = [
                "rows" => $rows,
                "total"=> $paginator->lastPage(),
                "page" => $paginator->currentPage(),
                "records" => $paginator->count(),
            ];

            return response()->json($data);

        } else {
            return view("game/conf/index");
        }
    }

    public function edit(Request $request) {

        $this->validate($request, [
            "KindID" => "int",
        ]);

        $KindID = $request->get("KindID");

        $oper = $request->get("oper");

        if ($oper == "del") {
            $KindID = $request->get("id");
            $logonAward = LogonAward::find($KindID);
            $logonAward->delete();
            $bankRuptOption = BankRuptOption::find($KindID);
            $bankRuptOption->delete();
            return response()->json(["success" => true]);
        }

        $logonAward = new LogonAward();
//         $bankRuptOption = new BankRuptOption();
        if ($oper == "edit") {
            $logonAward = LogonAward::find($KindID) ?: $logonAward;
//             $bankRuptOption = BankRuptOption::find($KindID) ?: $bankRuptOption;
        }

        $logonAward->KindID     = $KindID;
        $logonAward->GameName   = $request->get("GameName");
        $logonAward->LogonOne   = $request->get("LogonOne");
        $logonAward->LogonTwo   = $request->get("LogonTwo");
        $logonAward->LogonThree = $request->get("LogonThree");
        $logonAward->LogonFour  = $request->get("LogonFour");
        $logonAward->LogonFive  = $request->get("LogonFive");
        $logonAward->PCGiveGold = $request->get("PCGiveGold");
        $logonAward->RegisterGive = $request->get("RegisterGive");
        $logonAward->save();

//         $bankRuptOption->KindID     = $KindID;
//         $bankRuptOption->GameName   = $request->get("GameName");
//         $bankRuptOption->PCGiveGold = $request->get("PCGiveGold");
//         $bankRuptOption->save();

        return response()->json(["success" => true]);
    }
}
